<?php 
namespace app\Admin\controller;
use think\Controller;
use think\Db;
use think\Cookie;
use think\File;
class Base extends Controller
{
	public function __construct(){
		parent::__construct();
		$userid = Cookie::get('userid');
		if(empty($userid)){
			$this->error('请先登录...','Login/login');
		}
    	// $comRole = $this->checkPower();
        // print_r($comRole);die;
    	// if($comRole == NULL){
    	// 	$this->error('请向管理员申请权限','Login/login');
    	// }
    }

	public function checkPower(){
		// 获取当前用户的ID
    	$user_id = Cookie::get('userid');
    	$res = Db('user')->where("user_id = '$user_id'")->find();
    	// print_r($user_name);die;
    	// 获取当前用户对应的角色
        $roleIdList = Db('user_role')
			->alias('a')
			->join('__USER__ u','a.user_id = u.user_id')
			->join('__ROLE__ r','a.role_id = r.role_id')
			->where('u.user_id',$user_id)
			->select();
        // print_r($roleIdList);die;
			foreach ($roleIdList as $key => $v) {
				$roleIdList = $v;
			}
        $power = '/'.request()->module().'/'.request()->controller().'/'.request()->action();
        // print_r($power);die;
        //设置首页所有登录用户可访问
        if($power == "/admin/index/index"){
            return 1;
        }
        if ($res['user_name']=='www') {
        	return 1;
        }
        
    	// 查询当前(控制器/方法)对应的权限的ID
    	$power_id = Db('power')->where('power_name',$power)->value('power_id');
    	// print_r($power_id);die;
    	// 查询当前权限对应的所有角色
    	$roleId =Db('role_power')->where('power_id',$power_id)->column('role_id');
 		// 检测是否有公共项
 		$roles = array_intersect($roleIdList,$roleId );
 		return $roles;
	}


}